/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.base.thread
 * author:David
 * Date:2015年8月19日上午8:33:21
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.thread;

/**
 * @author David
 * @since 2015年8月19日 上午8:33:21
 * @version 
 * @since JDK 1.6
 */
public class TraditionalThreadSynchronized {
	
	public static void main(String[] args) {
		new TraditionalThreadSynchronized().init();
	}
	
	private void init(){
		final Outputer outputer=new Outputer();
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(800);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					outputer.output("David Xu");
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(800);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					outputer.output2("Jack D");
//					new Outputer().output2("Jack D");
//					outputer.output3("Jack D");
				}
			}
		}).start();
		
	}
	
	static class Outputer{
//		public void output(String name){
//			int len=name.length();
//			synchronized (this) {
//				for(int i=0;i<len;i++){
//					System.out.print(name.charAt(i)); 
//				}
//				System.out.println();
//			}
//		}
		public void output(String name){
			int len=name.length();
			synchronized (Outputer.class) {
				for(int i=0;i<len;i++){
					System.out.print(name.charAt(i)); 
				}
				System.out.println();
			}
		}
		
		public synchronized void output2(String name){
			int len=name.length();
			for(int i=0;i<len;i++){
				System.out.print(name.charAt(i)); 
			}
			System.out.println();
		}
		
		public static synchronized void output3(String name){
			int len=name.length();
			for(int i=0;i<len;i++){
				System.out.print(name.charAt(i)); 
			}
			System.out.println();
		}
	}
}

